package com.daring.mysql2maxkb;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.mashape.unirest.http.HttpResponse;
import com.mashape.unirest.http.Unirest;

import java.io.PrintStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

public class MaxKBUtils {
    static String AUTHORIZATION = "eyJ1c2VybmFtZSI6ImFkbWluIiwiaWQiOiJmMGRkOGY3MS1lNGVlLTExZWUtOGM4NC1hOGExNTk1ODAxYWIiLCJlbWFpbCI6IiIsInR5cGUiOiJVU0VSIn0:1s7QG2:L2QeoxCj6Kw1ODi1sok_i4Ywv9J2B_ptma-8Qt3om99";

    public static void request2AddSegmentation(HashMap<String, Object> value) {
        try {
            Unirest.setTimeouts(0, 0);
            HttpResponse<String> stringHttpResponse = Unirest.post("http://219.159.152.7:15421/api/dataset/352c5a92-1416-11ef-abd5-0242ac120002/document/352cb8ac-1416-11ef-abd5-0242ac120002/paragraph")
                    .header("AUTHORIZATION", AUTHORIZATION)
                    .header("Accept", "application/json, text/plain, */*")
                    .header("Accept-Language", "zh-CN,zh;q=0.9")
                    .header("Cache-Control", "no-cache")
                    .header("Connection", "keep-alive")
                    .header("Content-Type", "application/json")
                    .header("Origin", "http://219.159.152.7:15421")
                    .header("Pragma", "no-cache")
                    .header("Referer", "http://219.159.152.7:15421/ui/dataset/352c5a92-1416-11ef-abd5-0242ac120002/352cb8ac-1416-11ef-abd5-0242ac120002")
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0")
                    .body(JSON.toJSONString(value))
                    .asString();
            if (stringHttpResponse != null && stringHttpResponse.getStatus() == 200) {
                String resultStr = stringHttpResponse.getBody();
                Object parse = JSON.parse(resultStr);
                String message = (String) ((JSONObject) parse).get("message");
                System.out.println("message = " + message);
            }else if (stringHttpResponse != null && stringHttpResponse.getStatus() == 401) {
                reAuthorization();
                request2AddSegmentation(value);
            } else {
                System.err.println("HTTP request failed with status: " + (stringHttpResponse != null ? stringHttpResponse.getStatus() : "null"));
                request2AddSegmentation(value);
            }
        } catch (Exception e) {
            System.err.println("An error occurred during HTTP request: " + e.getMessage());
            e.printStackTrace();
            request2AddSegmentation(value);
        }
    }

    private static void reAuthorization() {
        try {
            Unirest.setTimeouts(0, 0);
            HttpResponse<String> stringHttpResponse = Unirest.post("http://219.159.152.7:15421/api/user/login")
                    .header("Accept", "application/json, text/plain, */*")
                    .header("Accept-Language", "zh-CN,zh;q=0.9")
                    .header("Cache-Control", "no-cache")
                    .header("Connection", "keep-alive")
                    .header("Content-Type", "application/json")
                    .header("Origin", "http://219.159.152.7:15421")
                    .header("Pragma", "no-cache")
                    .header("Referer", "http://219.159.152.7:15421/ui/login")
                    .header("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0")
                    .body("{\"username\":\"admin\",\"password\":\"@maxkbadmin123\"}")
                    .asString();
            if (stringHttpResponse != null && stringHttpResponse.getStatus() == 200) {
                String resultStr = stringHttpResponse.getBody();
                Object parse = JSON.parse(resultStr);
                AUTHORIZATION = (String) ((JSONObject) parse).get("data");
                System.out.println("AUTHORIZATION = " + AUTHORIZATION);
            } else {
                System.err.println("HTTP request failed with status: " + (stringHttpResponse != null ? stringHttpResponse.getStatus() : "null"));
                reAuthorization();
            }
        } catch (Exception e) {
            System.err.println("An error occurred during HTTP request: " + e.getMessage());
            e.printStackTrace();
            reAuthorization();
        }
    }
}
